import {Component, EventEmitter, Input, Output} from '@angular/core';
import {ApiProvider} from "../../providers/api/api";
import {LocalStorProvider} from "../../providers/local-stor/local-stor";
import {UnitSourceListPage} from "../../pages/unit-source-list/unit-source-list";
import {NavController} from "ionic-angular";

/**
 * Generated class for the ContinueComponent component.
 *
 * See https://angular.io/api/core/Component for more info on Angular
 * Components.
 */
@Component({
  selector: 'continue',
  templateUrl: 'continue.html'
})
export class ContinueComponent{

  user_id;
  student_id;
  startCourseInfo;
  curClassCourseId;

  // 继续听课的状态emit
  @Output() status = new EventEmitter<boolean>();

  @Input("data")
  set  lastestCourse(value) {
    this.startCourseInfo = value;
    if(this.startCourseInfo!=null)
    {
      this.status.emit(true);
    }
    else
    {
      this.status.emit(false);
    }
  }

  constructor(public http: ApiProvider, public storage: LocalStorProvider,public navCtrl: NavController) {
    this.storage.getKey("user_id").then(val=>{
      this.user_id=val;
      this.curStartCourse();
    });
  }

  /**
   * 当前授课课程单位信息
   */
  curStartCourse()
  {
    this.http.post("/course/getLastestStartCourse.do",{student_id:+this.user_id}).subscribe(item=>{
      if(item["Code"]=="11111111")
      {
        this.http.showToast(item["11111111"],"bottom");
      }
      else{
        this.startCourseInfo=item["LatestCourse"];
        if(this.startCourseInfo!=null)
        {
          this.status.emit(true);
          this.curClassCourseId=this.startCourseInfo["classCourseId"];
        } else {
          this.status.emit(false);
        }
      }
    })
  }

  /**
   * 继续听课
   * @param classcourseid
   * @param unitid
   * @constructor
   */
  ContinueCourse(classcourseid,unitid,teacherId)
  {
    localStorage.setItem("class_type","60001");
    this.navCtrl.push(UnitSourceListPage,{classCourseId:classcourseid,teacherId:teacherId,unitId:unitid,studentId:this.user_id,
      classType:"60001",sourceType:'xt'});
  }
}
